package com.css.fxfzysx.modules.vaolEngineering.repository.impl;

import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxWaterSupplyEntity;
import com.css.fxfzysx.modules.lifeline.vo.basic.DestructiveStateVo;
import com.css.fxfzysx.modules.vaolEngineering.entity.MtolProjectEntity;
import com.css.fxfzysx.modules.vaolEngineering.param.update.WaterSupplyParam;
import com.css.fxfzysx.modules.vaolEngineering.repository.WsfManagementRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
@Slf4j
public class WsfManagementRepositoryImpl implements WsfManagementRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveMtolProject(MtolProjectEntity it) {
        jdbcTemplate.update("insert into czt_main_table_of_lifeline_project(main_id,main_code,create_user,create_time,del_flag,rebar_type,fortification,years,business_object,province,city,county,equipment_type,site_classification,user_name,pipe_material,up_type,notes,floor,equipment_category,region,height)" +
                        "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", it.getMainId(), it.getMainCode(), it.getCreateUser(), it.getCreateTime(), it.getDelFlag(), it.getRebarType(), it.getFortification(), it.getYears(), it.getBusinessObject(), it.getProvince(), it.getCity(), it.getCounty(),
                it.getEquipmentType(), it.getSiteClassification(), it.getUserName(), it.getPipeMaterial(), it.getUpType(), it.getNotes(), it.getFloor(), it.getEquipmentCategory(), it.getRegion(), it.getHeight());
    }

    @Override
    public void updateMtolProject(MtolProjectEntity it) {
        String sql = "update czt_main_table_of_lifeline_project set update_user= :updateUser,update_time= :updateTime,rebar_type= :rebarType,fortification= :fortification,years= :years,province= :province,city= :city,county= :county," +
                "equipment_type= :equipmentType,site_classification= :siteClassification,pipe_material= :pipeMaterial,notes= :notes,floor = :floor,equipment_category= :equipmentCategory,region= :region,height= :height" +
                " where main_id= '" + it.getMainId() + "'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(it);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public MtolProjectEntity getMtolProjectById(String id) {
        String sql = "select * from czt_main_table_of_lifeline_project where main_id='" + id + "' and del_flag='0' order by create_time asc";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(MtolProjectEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public boolean saveWaterSupply(SmxWaterSupplyEntity entity) {
        String sql = "insert into czt_smx_water_supply values(:id,:waterSupplyId,:style,:material,:thickness,:shape,:radius,:slength,:length,:capacity,:depth,:height,:intensity,:year,:rebarType,:sectionWidth,:sectionHeight,:sectionBendingMoment,:steelStrain,:diameterOfTensileReinforcement,:reinforcementRatio,:shapeFactor1,:shapeFactor2,:averageStrain,:elasticModulus,:distance,:joint,:present,:quality,:surfaceShape1,:surfaceShape2,:diameter1,:diameter2,:strengthGrade,:ruptureStrength,:kinds1,:kinds2,:thicknessOfProtectiveLayer1,:thicknessOfProtectiveLayer2,:verticalReinforcement1,:verticalReinforcement2,:site,:liquefaction6,:liquefaction7,:liquefaction8,:liquefaction9,:landform6,:landform7,:landform8,ST_GeometryFromText(:geom,4490),:versionId,:tableId,:serialNumber,:result1,:result2,:result3,:result4,:result5,:result8,:result9,:disasterRateState,:disasterRateStateCorrect,:delFlag,:landform9,:result10,:createUser,:createTime,:updateUser,:updateTime,:extends1,:extends2,:extends3,:extends4,:extends5,:extends6,:extends7,:extends8,:extends9,:extends10,:longitude,:latitude)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public List<SmxWaterSupplyEntity> getByVersionId(String id) {
        try {
            String sql = "select * from czt_smx_water_supply where version_id = '"+id+"'";
            return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(SmxWaterSupplyEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void saveWaterSupplyResult(SmxWaterSupplyEntity entity) {
        jdbcTemplate.update("update czt_smx_water_supply set result1=?,result2=?,result3=?,result4=?,result5=?,result8=?,result9=?,result10=?,disaster_rate_state=?,disaster_rate_state_correct=?,result10_correct=? where id = ?",
                entity.getResult1(),entity.getResult2(),entity.getResult3(),entity.getResult4(),entity.getResult5(),entity.getResult8(),entity.getResult9(),entity.getResult10(),entity.getDisasterRateState(),entity.getDisasterRateStateCorrect(),entity.getResult10Correct(),entity.getId());
    }

    @Override
    public SmxWaterSupplyEntity getByTableId(String id) {
        try {
            String sql = "select * from czt_smx_water_supply where table_id = '"+id+"'";
            List<SmxWaterSupplyEntity> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SmxWaterSupplyEntity.class));
            return list.get(0);
        } catch (DataAccessException e) {
            log.error("error:{}",e.getMessage(),e);
            return null;
        }
    }

    @Override
    public void updateWsfManagementById(WaterSupplyParam entity) {
        jdbcTemplate.update("update czt_smx_water_supply set rebar_type=?,section_width=?,section_height=?,rupture_strength=?,section_bending_moment=?,steel_strain=?,diameter_of_tensile_reinforcement=?,reinforcement_ratio=?,shape_factor1=?,shape_factor2=?,average_strain=?,elastic_modulus=?,distance=?,disaster_rate_state=?,disaster_rate_state_correct=? where id =?",
                entity.getRebarType(),entity.getSectionWidth(),entity.getSectionHeight(),entity.getRuptureStrength(),entity.getSectionBendingMoment(),entity.getSteelStrain(),entity.getDiameterOfTensileReinforcement(),entity.getReinforcementRatio(),entity.getShapeFactor1(),entity.getShapeFactor2(),entity.getAverageStrain(),entity.getElasticModulus(),entity.getDistance(),entity.getDisasterRateState(),entity.getDisasterRateStateCorrect(),entity.getId());
    }

    @Override
    public DestructiveStateVo getByBasicInfo(SmxWaterSupplyEntity entity,String year) {
        try {
            StringBuilder sql = new StringBuilder("select * from czt_smx_basic_library_pool where del_flag = '0' and pool_shape = '" +entity.getShape() + "' and intensity = '" + entity.getIntensity() + "' and year = '" +year + "' and range = " + 0);
            return jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<>(DestructiveStateVo.class));
        } catch (DataAccessException e) {
            return new DestructiveStateVo();
        }
    }
}
